package com.example.controller;

import com.example.entity.Stayinfo;
import com.example.entity.Student;
import com.example.mapper.StayinfoMapper;
import com.example.mapper.StudentMapper;
import com.example.service.StayinfoService;
import com.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Stayinfo")
public class StayinfoConstoller {
    @Autowired
    private StayinfoMapper stayinfoMapper;

    @Autowired
    private StayinfoService stayinfoService;
    //修改和更新数据
    @PostMapping
    public Map<String, Object> save(@RequestBody Stayinfo stayinfo){
        return stayinfoService.saveStayinfo(stayinfo);
    }
    @GetMapping
    public List<Stayinfo> index(@RequestParam String stu_num) {
       return stayinfoMapper.findAllByid(stu_num);
    }

    //根据学号删除数据
    @DeleteMapping("/delete")
    public Map<String, Object> deleteByStuNum(@RequestParam String stu_num) {
        return stayinfoService.deleteByStuNum(stu_num);
    }

    @GetMapping("/page")
    public Map<String, Object> findPageWithOptionalParams(@RequestParam(required = false) Integer pageNum,
                                                          @RequestParam(required = false) Integer pageSize,
                                                          @RequestParam(required = false) String name) {
        pageNum = (pageNum - 1) * pageSize;
        List<Stayinfo> data = stayinfoMapper.selectPage(pageNum, pageSize, name);
        Integer total = stayinfoMapper.selectTotal(name);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    @GetMapping("/page1")
    public Map<String, Object> findPageWithOptionalParams1(@RequestParam(required = false) Integer pageNum,
                                                          @RequestParam(required = false) Integer pageSize,
                                                          @RequestParam(required = false) String name,
                                                           @RequestParam(required = false) String floor_num) {
        pageNum = (pageNum - 1) * pageSize;
        List<Stayinfo> data = stayinfoMapper.selectPage1(pageNum, pageSize, name,floor_num);
        Integer total = stayinfoMapper.selectTotal1(name,floor_num);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }
}
